/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function (height) {
  let ans = 0
  const stack = []
  let cur = 0
  while (cur < height.length) {
    while (stack.length !== 0 && height[cur] > stack[stack.length - 1]) {
      let index = stack.pop()
      let h = height[index]
      if (stack.length === 0) break
      let distance = cur - index - 1
      // let min = Math.min(height[temp], height[cur])
      ans += (height[cur] - top_height) * distance
    }
    stack.push(cur)
    cur++
  }
  return ans
}
console.log(trap([4, 2, 0, 3, 2, 5]))